﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ApiBetaSeries.Utils
{
    internal class UtilsXML
    {
        static internal bool GetBoolValue(XmlNode e, string name)
        {
            bool value = false;

            try
            {
                string str = e[name].FirstChild.Value;
                value = (str == "1" || str == "True");
            }
            catch (Exception)
            {
                value = false;
            }

            return value;
        }
        static internal string GetStringValue(XmlNode e, string name)
        {
            string value = "";
            try
            {
                value = e[name].FirstChild.Value;
            }
            catch (Exception)
            {
            }

            if (value == null)
            {
                value = "";
            }

            return value;
        }
        static internal int GetIntValue(XmlNode e, string name)
        {
            int value = -1;
            try
            {
                value = int.Parse(e[name].FirstChild.Value);
            }
            catch (Exception)
            {
                value = -1;
            }

            return value;
        }

        static internal double GetDoubleValue(XmlNode e, string name)
        {
            double value = -1;
            try
            {
                value = double.Parse(e[name].FirstChild.Value);
            }
            catch (Exception)
            {
                value = -1;
            }

            return value;
        }

        internal static DateTime GetDateValue(XmlNode e, string name)
        {
            DateTime value = DateTime.MinValue;
            try
            {
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long unixtime = long.Parse(e[name].FirstChild.Value);
                value = origin.AddSeconds(unixtime);
            }
            catch (Exception)
            {
                value = DateTime.MinValue;
            }
            return value;
        }

        internal static int GetEpisodeFromFormat(string format)
        {
            int episode = -1;
            try { episode = int.Parse( format.Substring(4, 2)); }
            catch (Exception) { episode = -1; }

            return episode;
        }
        internal static int GetSeasonFromFormat(string format)
        {
            int season = -1;
            try { season = int.Parse( format.Substring(1, 2)); }
            catch (Exception) { season = -1; }

            return season;
        }


        /// <summary>
        /// Get a Xml Document for the url
        /// </summary>
        /// <param name="url">relativ url on api (like /shows/display/dexter.xml)</param>
        /// <param name="param">param on request (like &title=, &token=)</param>
        /// <returns>Xml Document</returns>
        /// <exception cref="BetaSeriesException"></exception>
        /// <exception cref="WebException"></exception>
        public static XmlDocument GetUrl(string url, string param = "")
        {
            XmlDocument doc = new XmlDocument();
            url = BetaSeriesConfig.UrlApiBetaSeries + url;
            url += "?key=" + BetaSeriesConfig.ApiKey + param;

            try
            {
                doc.Load(@url);
            }
            catch (XmlException)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }


            XmlNode erreurs = doc["root"]["errors"];

            if (erreurs.HasChildNodes)
            {
                int errornumber = int.Parse(erreurs.FirstChild.Attributes["code"].Value);
                string message = erreurs.FirstChild.LastChild.Value;
                throw new BetaSeriesException(errornumber, message);
            }

            return doc;
        }


    }


}
